package hxg.thread.safethread;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author HeXiangGuang
 * @create 2021-06-24 21:58
 */
public class Demo1 {
    public static void main(String[] args) {
        List<String> arrayList = new ArrayList<>();
        List<String> sysList = Collections.synchronizedList(arrayList);
        for (int i = 0 ; i < 20 ;i++){
            int temp = i;
            new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int j = 0; j < 10; j++) {
                        //这个回报并发异常因为ArrayList是线程不安全的Exception in thread "Thread-1" java.util.ConcurrentModificationException
//                        arrayList.add(Thread.currentThread().getName()+"===="+temp+"====");
//                        System.out.println(arrayList.toString());
                        sysList.add(Thread.currentThread().getName()+"===="+temp+"====");
                        System.out.println(sysList.toString());
                    }
                }
            }).start();
        }
    }
}
